
package com.liferay.OTP;
import org.apache.commons.codec.binary.BinaryCodec;


import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.mail.model.*;

import com.liferay.mail.service.*;
import com.liferay.mail.util.*;
import com.liferay.mail.model.*;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.MimeTypesUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.*;
import javax.servlet.http.HttpServletRequest;
public class OTPServiceUtil {
	
public final static String OTP_HEADER_OPENTAG = "<OTPHEADER>";
public final static String OTP_HEADER_CLOSETAG = "</OTPHEADER>";
public final static String OTP_KEY_OPENTAG = "<OTP_KEY_ID>";
public final static String OTP_KEY_CLOSETAG = "</OTP_KEY_ID>";
public final static String OTP_CIPHER_OPENTAG = "<CIPHER_ID>";
public final static String OTP_CIPHER_CLOSETAG = "</CIPHER_ID>";
public final static  String OTP_BITSTREAM_OPENTAG = "<BITSTREAM>";
public final static String OTP_BITSTREAM_CLOSETAG = "</BITSTREAM>";



	public  static OTPCipherText OTPEncrypt(OTPkey k, OTPPlainText ut){
		
		String XORedBitStream = "";
		
		for (int i = 0;i<ut.getBitStream().length();i++){
			XORedBitStream += Integer.valueOf(ut.getBitStream().charAt(i)) ^ Integer.valueOf(k.getBitStream().charAt(i));
		}
					
		return new OTPCipherText(XORedBitStream);
	}
	
	public  static String OTPDecrypt(OTPkey k, OTPCipherText ct){
		String XORedBitStream = "";
		for (int i = 0;i<ct.getBitStream().length();i++){
			XORedBitStream += Integer.valueOf(ct.getBitStream().charAt(i)) ^ Integer.valueOf(k.getBitStream().charAt(i));
		}
	return BitStreamTotext(XORedBitStream);
	}

	public static String textToBitStream(String text){
		String bitStream ="";
		
		byte test_array[] = org.apache.commons.codec.binary.StringUtils.getBytesUsAscii(new StringBuffer(text).reverse().toString());
		bitStream = org.apache.commons.codec.binary.BinaryCodec.toAsciiString(test_array) ;
		return bitStream;
	}
	
	public static String BitStreamTotext(String bitStream){
		String streamBuffer ="";
		String stringBuffer = "";
		//popluate bitStream into a byte array;
		BinaryCodec bc = new BinaryCodec();
		byte byteArray [] =  bc.toByteArray(bitStream);
		for (int i =0;i<byteArray.length;i++){
			//System.out.println(byteArray[i]);
		}
		streamBuffer = org.apache.commons.codec.binary.StringUtils.newStringUsAscii(byteArray);
		stringBuffer=new StringBuffer(streamBuffer).reverse().toString();
		return stringBuffer ;
		//return streamBuffer ;		
	}

	public static OTPkey extractOTPKey(String messageString){
		String messageStr=messageString.replaceAll("<br />", "");
		int idIndexStart= messageStr.indexOf("<OTP_KEY_ID>")+12;
		int idIndexEnd = messageStr.indexOf("</OTP_KEY_ID>");
		
		int bitStreamIndexStart = messageStr.indexOf("<BITSTREAM>");
		int bitStreamIndexEnd = messageStr.indexOf("</BITSTREAM>");
		
		String keyBitStream="";
		String keyID = "";
		for (int i=bitStreamIndexStart;i<bitStreamIndexEnd;i++){
			if (messageStr.charAt(i)=='1'|messageStr.charAt(i)=='0'){
				keyBitStream += messageStr.charAt(i);
			}
		}
		
		for (int i=idIndexStart;i<idIndexEnd;i++){
			keyID += messageStr.charAt(i);
			
		}
		//System.out.println("Extracted KeyID: "+ keyID);
		OTPkey retKey =  	new OTPkey(keyBitStream);
		retKey.setID(keyID);
		return retKey;
	}
	
	public static OTPCipherText extractOTPCipherText(String messageString){
		String messageStr=messageString.replaceAll("<br />", "");
		
		int idIndexStart= messageStr.indexOf("<CIPHER_ID>")+11;
		int idIndexEnd = messageStr.indexOf("</CIPHER_ID>");
		int bitStreamIndexStart = messageStr.indexOf("<BITSTREAM>");
		int bitStreamIndexEnd = messageStr.indexOf("</BITSTREAM>");
		
		String ctBitStream="";
		String keyID = "";
		for (int i=bitStreamIndexStart;i<bitStreamIndexEnd;i++){
			if (messageStr.charAt(i)=='1'|messageStr.charAt(i)=='0'){
				ctBitStream += messageStr.charAt(i);
			}
		}
		
		for (int i=idIndexStart;i<idIndexEnd;i++){
			
				keyID += messageStr.charAt(i);
			
		}
		OTPCipherText retct =  	new OTPCipherText(ctBitStream);
		retct.setID(keyID);
		return retct;
	
	}

	public static boolean isOTPEncrypted(String messageBody){
		boolean isE = false;
		
		if(messageBody.contains(OTP_HEADER_OPENTAG) ) {
			isE= true;
		}
		
		return isE;
	}
	


}
